package com.guli.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.guli.commonutils.R;
import com.guli.vod.bean.ConstantPropertiesUtil;
import com.guli.vod.service.VideoService;
import com.guli.vod.utils.AliyunVodSDKUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName VideoAdminController
 * @Description TODO
 * @Author YinLiangTao Email:1517535831@qq.com
 * @Date 2022/4/7 16:36
 * @Version 1.0
 */
@Api("阿里云视频点播微服务")
@CrossOrigin //跨域
@RestController
@RequestMapping("/eduvod/video")
public class VideoAdminController {

    @Resource
    private VideoService videoService;

    @PostMapping("upload")
    public R uploadVideo(
            @ApiParam(name = "file", value = "文件", required = true)
            @RequestParam("file") MultipartFile file) throws Exception {
        String videoId = videoService.uploadVideo(file);
        return R.ok().message("视频上传成功").data("videoId", videoId);
    }

    //删除单个视频
    @DeleteMapping("/delete/{videoId}")
    public R removeVideo(@ApiParam(name = "videoId", value = "云端视频id", required
            = true) @PathVariable String videoId) {
        videoService.removeVideo(videoId);
        return R.ok().message("视频删除成功");
    }

    //删除多个视频
    @DeleteMapping("/delete-batch")
    public R removeVideos(@RequestParam("videoId") List videoId) {
        videoService.removeVideos(videoId);
        return R.ok().message("视频删除成功");
    }

    @GetMapping("get-play-auth/{videoId}")
    public R getVideoPlayAuth(@PathVariable("videoId") String videoId) throws
            Exception {
        //获取阿里云存储相关常量
        String accessKeyId = ConstantPropertiesUtil.getKeyid();
        String accessKeySecret = ConstantPropertiesUtil.getKeysecret();
        //初始化
        DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(accessKeyId,
                accessKeySecret);
        //请求
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        request.setVideoId(videoId);
        //响应
        GetVideoPlayAuthResponse response = client.getAcsResponse(request);
        //得到播放凭证
        String playAuth = response.getPlayAuth();
        //返回结果
        return R.ok().message("获取凭证成功").data("playAuth", playAuth);
    }
}
